package com.qeeka.sample04.handler;

import com.qeeka.sample04.model.UserInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * Created by Nathan.Liu on 2020/6/16.
 */
public class TimeClientHandler extends ChannelHandlerAdapter {

    public TimeClientHandler() {
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与服务器端链接建立成功...");
        UserInfo[] userInfos = userInfos();
        for (UserInfo info : userInfos) {
            ctx.writeAndFlush(info);
        }


    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("Now is : " + msg);

    }

    private UserInfo[] userInfos() {
        int loopNums = 10;
        UserInfo[] userInfos = new UserInfo[loopNums];
        for (int i = 0; i < loopNums; i++) {
            userInfos[i] = new UserInfo("名称_" + i, i);
        }
        return userInfos;
    }

}
